import time

from kunlun import lib

# 获取事件日志接口
log = lib.get_event_logger()


class FCT2:
    uut = None

    def setup(self):
        container_name = lib.get_container_name()
        log.debug(container_name)
        lib.add_test_data(sernum="1234567890", uuttype="DEMO", area="DEMO")
        ans = lib.ask_question("输入序列号：", tips="序列号以0001开始", image="demo/bing.jpg", height=4)
        log.debug("ask_question: %s", ans)
        ans = lib.ask_question("选择产品类型：", options=["12-3456-78", "98-7654-32"], image="demo/bing.jpg")
        log.debug("ask_question choose: %s", ans)
        ans = lib.ask_question(
            "选择产品类型：", options=["12-3456-78", "98-7654-32"], image="demo/bing.jpg", multiple=True
        )
        log.debug("ask_question multiple choose: %s", ans)
        ans = lib.ask_questions(
            ["输入序列号:", "输入MAC:", "输入编码:"], tips="三个问题的答案均不能为空", image="demo/bing.jpg"
        )
        log.debug("ask_questions: %s", ans)

    def run_case1(self):
        uut = lib.conn.ref
        uut.open()
        start_time = time.time()
        count = 0
        while time.time() - start_time < 5:
            count += 1
            lib.set_display5(f"测试次数: {count}")
            log.debug(f"第{count}次测试")
            uut.send("python3\r", expect=">>>", timeout=10)
            uut.send("import sys\r", expect=">>>", timeout=10)
            uut.send("sys.platform\r", expect=">>>", timeout=10)
            uut.send("exit()\r", expect=[">", "$"], timeout=10)
        uut.close()

    def run_case2(self):
        uut = lib.se.MCU
        uut.open()
        start_time = time.time()
        count = 0
        while time.time() - start_time < 5:
            count += 1
            lib.set_display4(f"测试次数: {count}")
            log.debug(f"第{count}次测试")
            uut.send("python3\r", expect=">>>", timeout=10)
            uut.send("import sys\r", expect=">>>", timeout=10)
            uut.send("sys.platform\r", expect=">>>", timeout=10)
            uut.send("exit()\r", expect=[">", "$"], timeout=10)
        uut.close()


# 定义入口函数, 每个文件只有一个入口函数。
def main_sequence():
    case = FCT2()
    # 增加一个根测试序列sequence，并设置名称为MAIN SEQ
    seq = lib.get_sequence_definition("MAIN SEQ")
    seq.add_step(case.setup, name="setup")
    seq.add_step(case.run_case1, name="Test Case1", in_parallel=True)
    seq.add_step(case.run_case2, name="Test Case2")

    # 返回根序列
    return seq
